package com.youpai.platform.vulture.service.handler.detection;

import com.youpai.platform.vulture.service.channel.VultureChannel;
import com.youpai.platform.vulture.service.constant.BizConstants;
import io.netty.util.Timeout;
import io.netty.util.TimerTask;

/**
 * @date: 2023/2/24 14:02
 * @author: Welles
 * @version: 1.0
 */
public class UnHandSharkConnectionCleanHandler implements TimerTask {

    private VultureChannel vultureChannel;

    public UnHandSharkConnectionCleanHandler(VultureChannel vultureChannel) {
        this.vultureChannel = vultureChannel;
    }

    @Override
    public void run(Timeout timeout) throws Exception {
        if(null == vultureChannel){
            return;
        }
        long connectTime = vultureChannel.getFirstConnectTime();
        // 如果连接已经可用，则不处理
        if (vultureChannel.isActive()) {
            vultureChannel = null;
            return;
        }
        // 如果已经超时未连接
        if (System.currentTimeMillis() - connectTime > BizConstants.UN_HAND_SHARK_CONNECTION_TIME_OUT) {
            vultureChannel.getChannel().disconnect();
            vultureChannel = null;
        }
    }
}
